package com.toe.user.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.StateEnum;
import com.toe.forum.service.ForumArticleService;
import com.toe.pojo.dto.user.UserReportArticleDTO;
import com.toe.pojo.entity.user.UserReportArticle;
import com.toe.pojo.vo.user.UserReportArticleVO;
import com.toe.user.webapi.mapper.UserReportArticleMapper;
import com.toe.user.service.UserReportArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@DubboService
@Service
public class UserReportArticleServiceImpl extends ServiceImpl<UserReportArticleMapper, UserReportArticle> implements UserReportArticleService {
    @Autowired
    private UserReportArticleMapper userReportArticleMapper;

    @DubboReference
    private ForumArticleService dubboArticleService;

    // 插入举报帖子信息
    @Override
    public void addReportArticle(UserReportArticleDTO userReportArticleDTO) {
        int rows = userReportArticleMapper.insertReportArticle(userReportArticleDTO);
        if(rows != 1) {
            throw new ToeServiceException(StateEnum.ERR_SERVER_BUSY,"服务器繁忙，插入帖子举报表失败");
        }
        // 调用国能方法将is_report改为1
        dubboArticleService.updateArticleIsReport(userReportArticleDTO.getArticleId(),1);
    }

    // 分页查询举报帖子
    @Override
    public JsonPage<UserReportArticleVO> pageReportArticle(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserReportArticleVO> pageArticles = userReportArticleMapper.selectReportArticle();
        return JsonPage.restPage(new PageInfo<>(pageArticles));
    }

    // 帖子举报失败修改is_handled字段为2
    @Override
    public void forbidArticle(Long id) {
        int rows = userReportArticleMapper.forbidIsHandled(id);
        if(rows != 1) {
            throw new ToeServiceException(StateEnum.ERR_SERVER_BUSY,"服务器繁忙");
        }
        dubboArticleService.adminUpdateArticleIsReport(id, 0) ;
    }

    // 帖子举报成功修改is_handled字段为1
    @Override
    public void passArticle(Long id) {
        int rows = userReportArticleMapper.passIsHandled(id);
        if(rows != 1) {
            throw new ToeServiceException(StateEnum.ERR_SERVER_BUSY,"服务器繁忙");
        }
        dubboArticleService.adminUpdateArticleIsReport(id, 1);
        dubboArticleService.deleteArticleByIds(id);
    }

}
